home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / music_utilities / pt030.dms / pt030.adf / Less / Src / prompt.c < prev    next >
C/C++ Source or Header  |  1987-06-15  |  4KB  |  221 lines

  1. /*
  2.  * Prompting and other messages.
  3.  * There are three flavors of prompts, SHORT, MEDIUM and LONG,
  4.  * selected by the -m/-M options.
  5.  * A prompt is either a colon or a message composed of various
  6.  * pieces, such as the name of the file being viewed, the percentage
  7.  * into the file, etc.
  8.  */
  9.  
  10. #include "less.h"
  11. #include "position.h"
  12.  
  13. extern int pr_type;
  14. extern int ispipe;
  15. extern int hit_eof;
  16. extern int new_file;
  17. extern int sc_width;
  18. extern char current_file[];
  19. extern int ac;
  20. extern char **av;
  21. extern int curr_ac;
  22.  
  23. /*
  24.  * Prototypes for the three flavors of prompts.
  25.  * These strings are expanded by pr_expand().
  26.  */
  27. char *prproto[] = { 
  28.     "fo",        /* PR_SHORT */
  29.     "foP",        /* PR_MEDIUM */
  30.     "Fobp"        /* PR_LONG */
  31. };
  32.  
  33. static char message[200];
  34. static char *mp;
  35.  
  36.     static void
  37. setmp()
  38. {
  39.     mp = message + strlen(message);
  40. }
  41.  
  42. /*
  43.  * Append the name of the current file (to the message buffer).
  44.  */
  45.     static void
  46. ap_filename()
  47. {
  48.     if (ispipe)
  49.         return;
  50.     strtcpy(mp, current_file, &message[sizeof(message)] - mp);
  51.     setmp();
  52. }
  53.  
  54. /*
  55.  * Append the "file N of M" message.
  56.  */
  57.     static void
  58. ap_of()
  59. {
  60.     if (ac <= 1)
  61.         return;
  62.     sprintf(mp, " (file %d of %d)", curr_ac+1, ac);
  63.     setmp();
  64. }
  65.  
  66. /*
  67.  * Append the byte offset into the current file.
  68.  */
  69.     static void
  70. ap_byte()
  71. {
  72.     POSITION pos, len;
  73.  
  74.     pos = position(BOTTOM_PLUS_ONE);
  75.     if (pos == NULL_POSITION)
  76.         pos = ch_length();
  77.     if (pos != NULL_POSITION)
  78.     {
  79.         sprintf(mp, " byte %ld", (long)pos);
  80.         setmp();
  81.         len = ch_length();
  82.         if (len > 0)
  83.         {
  84.             sprintf(mp, "/%ld", (long)len);
  85.             setmp();
  86.         }
  87.     }
  88. }
  89.  
  90. /*
  91.  * Append the percentage into the current file.
  92.  * If we cannot find the percentage and must_print is true,
  93.  * use the byte offset.
  94.  */
  95.     static void
  96. ap_percent(must_print)
  97. {
  98.     POSITION pos,len;
  99.  
  100.     pos = position(BOTTOM_PLUS_ONE);
  101.     len = ch_length();
  102.     if (len > 0 && pos != NULL_POSITION)
  103.     {
  104.         sprintf(mp, " (%ld%%)", (100 * (long)pos) / len);
  105.         setmp();
  106.     } else if (must_print)
  107.         ap_byte();
  108. }
  109.  
  110. /*
  111.  * Append the end-of-file message.
  112.  */
  113.     static void
  114. ap_eof()
  115. {
  116.     strcpy(mp, " (END)");
  117.     setmp();
  118.     if (curr_ac + 1 < ac)
  119.     {
  120.         sprintf(mp, " - Next: %s", av[curr_ac+1]);
  121.         setmp();
  122.     }
  123. }
  124.  
  125. /*
  126.  * Construct a message based on a prototype string.
  127.  */
  128.     static char *
  129. pr_expand(proto, maxwidth)
  130.     char *proto;
  131.     int maxwidth;
  132. {
  133.     register char *p;
  134.  
  135.     mp = message;
  136.  
  137.     for (p = proto;  *p != '\0';  p++)
  138.     {
  139.         if (maxwidth > 0 && mp >= message + maxwidth)
  140.         {
  141.             /*
  142.              * Truncate to the screen width.
  143.              * {{ This isn't very nice. }}
  144.              */
  145.             mp = message + maxwidth;
  146.             break;
  147.         }
  148.         switch (*p)
  149.         {
  150.         case 'f':
  151.             if (new_file)
  152.                 ap_filename();
  153.             break;
  154.         case 'F':
  155.             ap_filename();
  156.             break;
  157.         case 'o':
  158.             if (new_file)
  159.                 ap_of();
  160.             break;
  161.         case 'O':
  162.             ap_of();
  163.             break;
  164.         case 'b':
  165.             ap_byte();
  166.             break;
  167.         case 'p':
  168.             if (!hit_eof)
  169.                 ap_percent(0);
  170.             break;
  171.         case 'P':
  172.             if (!hit_eof)
  173.                 ap_percent(1);
  174.             break;
  175.         case '<':
  176.             while (*++p != '>')
  177.             {
  178.                 if (*p == '\0')
  179.                 {
  180.                     p--;
  181.                     break;
  182.                 }
  183.                 *mp++ = *p;
  184.             }
  185.             break;
  186.         default:
  187.             *mp++ = *p;
  188.             break;
  189.           }
  190.     }
  191.     if (hit_eof)
  192.         ap_eof();
  193.  
  194.     new_file = 0;
  195.     if (mp == message)
  196.         return (NULL);
  197.     *mp = '\0';
  198.     return (message);
  199. }
  200.  
  201. /*
  202.  * Return a message suitable for printing by the "=" command.
  203.  */
  204.     public char *
  205. eq_message()
  206. {
  207.     return (pr_expand("FObp", 0));
  208. }
  209.  
  210. /*
  211.  * Return a prompt.
  212.  * This depends on the prompt type (SHORT, MEDIUM, LONG), etc.
  213.  * If we can't come up with an appropriate prompt, return NULL
  214.  * and the caller will prompt with a colon.
  215.  */
  216.     public char *
  217. pr_string()
  218. {
  219.     return (pr_expand(prproto[pr_type], sc_width-2));
  220. }
  221.